package com.sense.common.exception;

import com.sense.common.lang.OpenCode;
import com.sense.common.lang.RestfulResponse;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.Set;
import java.util.stream.Collectors;

@RestControllerAdvice
public class ExceptionAdvanceHandler {


    @ExceptionHandler(value = NullPointerException.class)
    public RestfulResponse nullPointerException(NullPointerException e) {
        return RestfulResponse.fail(OpenCode.NULL_POINT_ERROR.getCode(),e.getMessage());
    }



    @ExceptionHandler(value = IllegalArgumentException.class)
    public RestfulResponse handleIllegalArgumentException(IllegalArgumentException e) {
        return RestfulResponse.fail(OpenCode.PARAMS_ERROR.getCode(),e.getMessage());
    }


    @ExceptionHandler(value = NoHandlerFoundException.class)
    public RestfulResponse noHandlerFoundException(NoHandlerFoundException e) {
        return RestfulResponse.fail(OpenCode.NO_HANDLER_EXCEPTION.getCode(),e.getMessage());
    }


    @ExceptionHandler(value = BizException.class)
    public RestfulResponse baseException(BizException e) {
        return RestfulResponse.fail(e.getCode(),e.getMessage());
    }



    @ExceptionHandler(MethodArgumentNotValidException.class)
    public RestfulResponse bindException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        Set<String> errSet = bindingResult.getAllErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.toSet());
        String errorMessage = String.join(", ", errSet);
        return RestfulResponse.fail(OpenCode.PARAMS_VALID_ERROR.getCode(),errorMessage);
    }


    /**
     * 参数绑定错误
     *
     * @param ex
     * @return
     */
    //@ExceptionHandler(value = BindException.class)
    //public RestfulResponse exception(BindException ex) {
    //    String defaultMessage = Objects.requireNonNull(ex.getBindingResult().getFieldError()).getDefaultMessage();
    //    return RestfulResponse.fail(defaultMessage);
    //}


    /**
     * HttpRequestMethodNotSupportedException
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public RestfulResponse httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return RestfulResponse.fail(OpenCode.METHOD_NOT_SUPPORT.getCode(),e.getMessage());
    }


    @ExceptionHandler(value = Exception.class)
    public RestfulResponse exception(Exception e) {
        return  RestfulResponse.fail(OpenCode.SERVER_ERROR.getCode(),e.getMessage());
    }


}